from home.models import Article, ArticleCategory


def common_data(request):
    # 1、获取所有分类信息数据
    categories = ArticleCategory.objects.all()
    # 2、接收用户点击的分类id
    cat_id = request.GET.get('cat_id', 1)
    # 3、根据分类id进行分类的查询
    try:
        category = ArticleCategory.objects.get(id=cat_id)
    except ArticleCategory.DoesNotExist:
        category = ArticleCategory.objects.get(id=1)
    news_art = Article.objects.order_by('-create_time')[:2]
    hot_tags = Article.objects.values('tags').order_by('-total_views').distinct()[:9]

    return {
        "news_art":news_art,
        "hot_tags":hot_tags,
        'categories': categories,
        'category': category,
        'cat_id': cat_id
    }